//
//  HRMessageWhoLookViewController.m
//  SpeedDating
//
//  Created by ISU1 on 17/2/8.
//  Copyright © 2017年 isu. All rights reserved.
//

#import "FriendWhoLookViewController.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "FriendWhoLookTableViewCell.h"
#import "FriendRemindWhoController.h"

@interface FriendWhoLookViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, assign) NSInteger index;

@property (nonatomic, strong) NSArray *friendArray1;

@property (nonatomic, strong) NSArray *friendArray2;

@property (nonatomic, strong) NSArray *addUserArray1;

@property (nonatomic, strong) NSArray *addUserArray2;

@end

@implementation FriendWhoLookViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.addUserArray1 = @[];
    self.addUserArray2 = @[];
    self.friendArray1 = @[];
    self.friendArray2 = @[];
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.navigationItem setTitle:@"谁可以看"];
    
    UIButton *commitBtn = [UIButton new];
    [commitBtn setTitle:@"完成" forState:UIControlStateNormal];
    [commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    commitBtn.frame = CGRectMake(0, 0, 40, 40);
    commitBtn.titleLabel.font = [UIFont systemFontOfSize:15.0f];
    [commitBtn addTarget:self action:@selector(clickRightButton) forControlEvents:UIControlEventTouchUpInside];
    [commitBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:commitBtn];
  
    _index = 0;
    if (!_tableView) {
        _tableView = [[UITableView alloc] init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.view).offset(0);
            make.left.right.bottom.equalTo(self.view);
        }];
        _tableView.tableFooterView =[UIView new];
        [_tableView registerClass:[FriendWhoLookTableViewCell class] forCellReuseIdentifier:@"WhoLookCell"];
    }
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    if (_friendArray1.count==0&&_friendArray2.count==0) {
        _index = 0;
    }
    [_tableView reloadData];
}

- (void)clickRightButton {
    NSDictionary *dic = @{};
    if (_friendArray1.count||_friendArray2.count) {
        if (_friendArray1.count) {
            dic = @{@"friendArray":_addUserArray1,@"seeType":@(_index+1)};
        } else {
            dic = @{@"friendArray":_addUserArray2,@"seeType":@(_index+1)};
        }
    } else {
        dic = @{@"seeType":@(_index+1)};
    }
    if (self.jump) {
        self.jump(dic);
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 2) {
        return [self.tableView cellHeightForIndexPath:indexPath model:_friendArray1 keyPath:@"userArray" cellClass:[FriendWhoLookTableViewCell class] contentViewWidth:[self cellContentViewWith]];
    }
    if (indexPath.row == 3) {
        return [self.tableView cellHeightForIndexPath:indexPath model:_friendArray2 keyPath:@"userArray" cellClass:[FriendWhoLookTableViewCell class] contentViewWidth:[self cellContentViewWith]];
    }
    return 51;
}

- (CGFloat)cellContentViewWith {
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    // 适配ios7
    if ([UIApplication sharedApplication].statusBarOrientation != UIInterfaceOrientationPortrait && [[UIDevice currentDevice].systemVersion floatValue] < 8) {
        width = [UIScreen mainScreen].bounds.size.height;
    }
    return width;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray *array = @[@"公开",@"秘密",@"部分可见",@"不给谁看"];
    NSArray *arrbottom = @[@"所有朋友可见",@"仅自己可见",@"选中的朋友可见",@"选中的朋友不可见"];
    FriendWhoLookTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WhoLookCell"];
    [cell useCellFrameCacheWithIndexPath:indexPath tableView:tableView];
    if (indexPath.row == 2 || indexPath.row == 3) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    if (indexPath.row == _index) {
        cell.isSelect = YES;
    } else {
        cell.isSelect = NO;
    }
    cell.title = array[indexPath.row];
    cell.titleBottom =arrbottom[indexPath.row];
    switch (indexPath.row) {
        case 0:
            cell.userArray = @[];
            break;
        case 1:
            cell.userArray = @[];
            break;
        case 2:
            cell.userArray = _friendArray1;
            break;
        case 3:
            cell.userArray = _friendArray2;
            break;
        default:
            break;
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 2) {
        FriendRemindWhoController *SFVC = [FriendRemindWhoController new];
        SFVC.addUserArray = self.addUserArray1.mutableCopy;
        SFVC.titleString = @"部分可见";
        SFVC.selectedFriend = ^(NSArray *friendArray, NSArray *addUserArray) {
            _addUserArray1 = addUserArray;
            _friendArray1 = friendArray;
            _addUserArray2 = @[];
            _friendArray2 = @[];
            _index = indexPath.row;
            [tableView reloadData];
        };
        [self.navigationController pushViewController:SFVC animated:YES];
    } else if (indexPath.row == 3) {
        FriendRemindWhoController *SFVC = [FriendRemindWhoController new];
        SFVC.addUserArray = self.addUserArray2.mutableCopy;
        SFVC.titleString = @"部分不可见";
        SFVC.selectedFriend = ^(NSArray *friendArray, NSArray *addUserArray) {
            _addUserArray2 = addUserArray;
            _friendArray2 = friendArray;
            _addUserArray1 = @[];
            _friendArray1 = @[];
            _index = indexPath.row;
            [tableView reloadData];
        };
        [self.navigationController pushViewController:SFVC animated:YES];
    } else {
        _addUserArray2 = @[];
        _friendArray2 = @[];
        _addUserArray1 = @[];
        _friendArray1 = @[];
        _index = indexPath.row;
        [tableView reloadData];
    }
}

@end
